﻿using Abp;
using Abp.Domain.Repositories;
using FwProject.Product.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.Product;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FwProject.Product
{
    public class ProductTypeSpceAppService : FwProjectAppServiceBase, IProductTypeSpceAppService
    {
        private readonly IRepository<ProductTypeSpce, Guid> _repositoryProductTypeSpce;
        private readonly IRepository<Spce, Guid> _repositorySpce;
        private IGuidGenerator _guid { get; set; }
        public ProductTypeSpceAppService(IRepository<ProductTypeSpce, Guid> repositoryProductTypeSpce, IRepository<Spce, Guid> repositorySpce)
        {
            _repositoryProductTypeSpce = repositoryProductTypeSpce;
            _repositorySpce = repositorySpce;
            _guid = SequentialGuidGenerator.Instance;
        }


        public async Task UpdatedBatchAsync(Guid id, ProductTypeSpceEnterDto input)
        {
            var original = _repositoryProductTypeSpce.GetAll().Where(o => o.ProductTypeid == id).ToList();
            if (!string.IsNullOrWhiteSpace(input.Ids))
            {
                Guid temp;
                //获取提交的新规格Id集合
                var spceids = input.Ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(o => { Guid.TryParse(o, out temp); return temp; });
                if (spceids.Any())//判断是否有元素
                {
                    if (original.Count > 0)
                    {
                        //获取新规格与原规格之间的差异数据
                        var removes = original.Where(o => spceids.Any(r => r == o.SpceId) == false).ToList();
                        if (removes?.Count > 0)
                        {
                            for (int i = 0; i < removes.Count; i++)//执行删除操作
                                await _repositoryProductTypeSpce.DeleteAsync(removes[i]);
                        }
                    }
                    //获取添加的元素
                    var adds = spceids.Where(r => original.Any(o => o.SpceId == r) == false);
                    foreach (var a in adds)
                        await _repositoryProductTypeSpce.InsertAndGetIdAsync(new ProductTypeSpce { Id = _guid.Create(), SpceId = a, ProductTypeid = id });
                }
            }
            else
            {
                if (original.Count > 0)
                {
                    for (int i = 0; i < original.Count; i++)//执行删除操作
                        await _repositoryProductTypeSpce.DeleteAsync(original[i]);
                }
            }

        }

        public List<ProductTypeSpceOutput> GetOutputDto(Guid id)
        {
            var spces = _repositorySpce.GetAll();
            var query = _repositoryProductTypeSpce.GetAll().Where(o => o.ProductTypeid == id);

            var list = query.Join(spces, q => q.SpceId, b => b.Id, (q, b) => new ProductTypeSpceOutput()
            {
                Id = q.Id,
                SpceId = b.Id,
                 SpceName = b.SpceName
            }).ToList();

            return list;
        }
    }

}
